// 定义一个serve对假数据增删改查;
import { Coffee } from './entities/coffee.entity';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';

@Injectable()
export class CoffeesService {
  private coffees: Coffee[] = [
    {
      id: 1,
      name: '假数据',
      brand: '品牌',
      flavors: ['风味', '风味2'],
    },
  ]; //使用这个临时作为伪数据
  findAll() {
    return this.coffees;
  }

  findOne(id: string) {
    // throw '随机错误';
    const coffee = this.coffees.find((item) => item.id === +id);
    if (!coffee) {
      throw new HttpException(`Coffee #${id} not fount`, HttpStatus.NOT_FOUND);
    }
    return coffee;
  }
  created(createCoffeeDto: any) {
    return this.coffees.push(createCoffeeDto);
  }
  update(id: string, updateCoffeeDto: any) {
    const existingCoffee = this.findOne(id);
    if (existingCoffee) {
    }
  }
  remove(id: string) {
    const coffeeIndex = this.coffees.findIndex((item) => item.id === +id);
    if (coffeeIndex >= 0) {
      this.coffees.splice(coffeeIndex, 1);
    }
  }
}
